Skip to content

Reduce INFORMATION_SCHEMA calls in BigQuery get_tables_by_pattern_sql#1084

Open
emilmar wants to merge 1 commit into
dbt-labs:mainfrom
emilmar:emil/simplify-bq-relations-by-pattern
Open

Reduce INFORMATION_SCHEMA calls in BigQuery get_tables_by_pattern_sql#1084
emilmar wants to merge 1 commit into
dbt-labs:mainfrom
emilmar:emil/simplify-bq-relations-by-pattern

Conversation

@emilmar

@emilmar emilmar commented May 18, 2026

Copy link
Copy Markdown

Problem

On BigQuery, dbt_utils.get_relations_by_pattern (via get_tables_by_pattern_sql) runs two kinds of INFORMATION_SCHEMA queries whenever the schema_pattern contains %:

  1. An INFORMATION_SCHEMA.SCHEMATA lookup to enumerate matching datasets.
  2. A UNION ALL of one dataset-scoped INFORMATION_SCHEMA.TABLES query per match.

This is more work than necessary. The region-qualified `project`.`region-X`.INFORMATION_SCHEMA.TABLES view already exposes table_schema, so the schema match can happen inside the same query as the table match.

Solution

When target.location is set, query the region-qualified INFORMATION_SCHEMA.TABLES view once and filter table_schema directly. The SCHEMATA call and the UNION ALL go away.

The existing SCHEMATA + iterate path is kept as a fallback for profiles without a configured location, so behaviour is unchanged for those users. The single-schema (no %) path is also unchanged. Since BigQuery is highly dependent on region, I assume most users set the target.location value. The previous version did not allow region override so it is still an improvement over that. I think we might even remove the fallback, and allow it to error instead.

Note on CI coverage

integration_tests/profiles.yml doesn't set location, so the existing test_get_relations_by_pattern model exercises the fallback path, not the new one. Happy to add location: US to the integration profile if maintainers want the fast path covered in CI.

Checklist

  • I have read the contributing guide and understand what's expected of me
  • I have run this code in development and it appears to resolve the stated issue
  • This PR includes tests, or tests are not required/relevant for this PR
  • I have updated the README.md (if applicable)

… call

When `target.location` is set, use the region-qualified
INFORMATION_SCHEMA.TABLES view so `table_schema` can be filtered
directly. That replaces the SCHEMATA lookup + per-dataset UNION ALL
with a single query. Falls back to the existing path when no
location is configured, so behaviour is unchanged for those users.
@emilmar

emilmar commented May 18, 2026

Copy link
Copy Markdown
Author

This PR was created after a discussion on undocumented rate limits in BigQuery that were hit due to dbt Fusion effectiveness.

@emilmar emilmar marked this pull request as ready for review May 18, 2026 13:38
@emilmar emilmar requested a review from a team as a code owner May 18, 2026 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant